/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command.media
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import multimedia.control.business.MediaDelegate;
	import multimedia.control.event.media.GetMediaEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.MediaVO;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;

	public class GetMediaCommand implements ICommand, IResponder
	{

		private var event:GetMediaEvent;

		public function execute(event:CairngormEvent):void
		{
			var e:GetMediaEvent=event as GetMediaEvent;
			this.event=e;
			var service:MediaDelegate=new MediaDelegate(this);
			service.getMedia(e.approved, e.added_by);
		}

		public function result(data:Object):void
		{
			var media:ArrayCollection=data.result as ArrayCollection;

			var tmp:Array=new Array();
			var object:Object;
			for (var i:int=0; i < media.length; i++)
			{
				for(var j:int = 0; j < (media.getItemAt(i) as ArrayCollection).length; j++)
				{
					var item:Object = (media.getItemAt(i) as ArrayCollection).getItemAt(j);
					if(item == null)
					{
						trace("item is null, will delete it from list");
						(media.getItemAt(i) as ArrayCollection).removeItemAt(j);
					}
				}
				var type:String=((media.getItemAt(i)as ArrayCollection).getItemAt(0)as MediaVO).type;
				if(type == "text")
				{
					var lastFeed_id:int = -1;
					var lastFeed_title:String = "";
					var categories:Object = new Object();
					categories['cats'] = new ArrayCollection();
					var children:ArrayCollection = new ArrayCollection();
					var feedGroup:ArrayCollection = new ArrayCollection();
					var textMedia:ArrayCollection = media.getItemAt(i)as ArrayCollection;
					for(var k:int = 0; k < textMedia.length; k++)
					{
						//trace((textMedia.getItemAt(k) as MediaVO).feed);
						if(lastFeed_id != (textMedia.getItemAt(k) as MediaVO).feed.feed_id)
						{
							if(feedGroup.length != 0)
							{
								children.addItem({label: lastFeed_title, children:feedGroup});
							}
							feedGroup = new ArrayCollection();
							lastFeed_id = (textMedia.getItemAt(k) as MediaVO).feed.feed_id;
							lastFeed_title = (textMedia.getItemAt(k) as MediaVO).feed.title; 
							lastFeed_title = lastFeed_title == null ? "manuell erstellt" : lastFeed_title;
							if(lastFeed_title == "manuell erstellt" ) 
							{
								categories['manuell'] = feedGroup;
							}
						}
						if((textMedia.getItemAt(k) as MediaVO).category == null)
						{
							feedGroup.addItem(textMedia.getItemAt(k));
						} else
						{
							var tmpMedia:MediaVO = textMedia.getItemAt(k) as MediaVO;
							var t:String = tmpMedia.category.name;
							if(categories[t] == null)
							{
								categories[t] = new ArrayCollection();
								feedGroup.addItem({label: t, children: categories[t]});
							}
							(categories[t] as ArrayCollection).addItem(tmpMedia);
						}
					}
					if(feedGroup.length != 0)
					{
						children.addItem({label: lastFeed_title, children:feedGroup});
					}
					object = {label:ModelLocator.mediaNames[type], children:children};
				} else
				{
					object = {label:ModelLocator.mediaNames[type], children:media.getItemAt(i)};
				}
				
				tmp.push(object);
			}


			if (event.approved)
			{
				ModelLocator.getInstance().approvedMedia=tmp;
			}
			else
			{
				ModelLocator.getInstance().nonApprovedMedia=tmp;
			}
		}

		public function fault(info:Object):void
		{
		}

	}
}